﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using DAL.Entity;
using DAL.Persistencia;

namespace NewTierra.Pages.ashx
{
   
    public class SearchTransfers : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            try
            {

                ServicoDAL ftd = new ServicoDAL();                

                var term = context.Request.QueryString["term"].ToString();
                var Cidade = context.Request.QueryString["param1"].ToString();

                context.Response.Clear();
                context.Response.ContentType = "application/json";

                List<S_Servicos> servicos = new List<S_Servicos>();

                if (!Cidade.Equals(""))
                {
                    servicos = ftd.ListarTodosTrf(term, new CidadeDAL().ObterPorNome(Cidade).CID_id);
                }
                else
                {
                    servicos = ftd.ListarTodosTrf(term);
                }

                List<string> lista = new List<string>();


                foreach (S_Servicos p in servicos)
                {
                    lista.Add(p.Servicos_Nome);
                }

                var search = lista;

                JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                string json = jsSerializer.Serialize(search);
                context.Response.Write(json);
                context.Response.End();

            }
            catch 
            {                
                throw;
            }            
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}